package com.xindaoapp.happypet;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;

import com.xindaoapp.happypet.utils.CommonUtil;


/**
 * 引导页面
 * 
 * @author Yangqing
 * 
 */
public class GuideActivity extends Activity {
    private ViewPager guideViewpager;// 引导页viewPager
    private View button;// 进入应用
    /** 导航提示符 */
    private SharedPreferences sp;

    // private final int[] mGuideImageViews = new int[] {R.drawable.guide01, R.drawable.guide02,
    // R.drawable.guide03, R.drawable.guide04};

    private List<View> views;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_guide);


        View v1 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.guide1, null);
        View v2 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.guide2, null);
        View v3 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.guide3, null);
        View v4 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.guide4, null);
        views = new ArrayList<View>();

        views.add(v1);
        views.add(v2);
        views.add(v3);
        views.add(v4);


        sp = getSharedPreferences("userconfig", MODE_PRIVATE);
        guideViewpager = (ViewPager) findViewById(R.id.guideViewpager);
        button = findViewById(R.id.button);



        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sp.edit()
                        .putBoolean("isfirst" + CommonUtil.getVersionCode(GuideActivity.this),
                                false).commit();
                Intent intent =
                        new Intent(GuideActivity.this, com.xindaoapp.happypet.ui.MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        GuideViewPagerAdapter adapter = new GuideViewPagerAdapter();
        guideViewpager.setAdapter(adapter);
        guideViewpager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                if (position == views.size() - 1) {
                    button.setVisibility(View.VISIBLE);
                } else {
                    button.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int position) {

            }
        });

    }

    class GuideViewPagerAdapter extends PagerAdapter {

        public GuideViewPagerAdapter() {}

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            container.addView(views.get(position));
            return views.get(position);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        System.gc();
    }

}
